<?php
namespace Commons;

class MyCrypt
{
    /**
     * @var string 初始化向量
     */
    private $iv;
    /**
     * @var string 密码学方式
     */
    private $method;

    /**
     * @var string key
     */
    private $key;

    public function __construct()
    {
        $salt         = 'wewechat';
        $this->key    = hash('sha256', $salt, true);
        $this->method = 'AES-256-CBC';
        $this->iv     = substr($this->key, 0, 16);
    }

    /**
     * 加密数据
     *
     * @param $input
     *
     * @return string
     */
    public function encrypt($input)
    {
        return base64_encode(openssl_encrypt($input, $this->method, $this->key, 0, $this->iv));
    }

    /**
     * 解密数据
     *
     * @param $input
     *
     * @return string
     */
    public function decrypt($input)
    {
        return trim(openssl_decrypt(base64_decode($input), $this->method, $this->key, 0, $this->iv));
    }
}
